﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DetectiveCluedo
{
    public partial class GameGridForm : Form
    {
        public GameGridForm()
        {
            InitializeComponent();

            dataGridView1.AutoGenerateColumns = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            refreshInvestigation();
        }

        private void GameGridForm_Load(object sender, EventArgs e)
        {
            CluedoGame.Changed += new ChangedEventHandler(CluedoGame_Changed);

            refreshInvestigation();
        }

        void CluedoGame_Changed()
        {
            refreshInvestigation();
        }


        private void refreshInvestigation()
        {
            // Grid
            DataGridViewCellStyle solutionStyle = new DataGridViewCellStyle();
            solutionStyle.Font = new Font(dataGridView1.Font.FontFamily, dataGridView1.Font.Size, FontStyle.Bold);


            bool solutionFound = CluedoGame.Investigate();

            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            dataGridView1.EnableHeadersVisualStyles = false;

            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "_card", HeaderText = "Card\\Player", Frozen = true });

            foreach (Player p in CluedoGame.Players)
            {
                var c = new DataGridViewImageColumn() { Name = p.Name, HeaderText = p.Name };
                c.DefaultCellStyle.NullValue = null;
                c.HeaderCell.Style.BackColor = p.Color;
                dataGridView1.Columns.Add(c);
            }

            foreach (Card card in CluedoGame.Cards)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dataGridView1);
                row.Cells[0].Value = card.Name;

                for (int i = 0; i < CluedoGame.Players.Length; i++)
                {
                    var pci = CluedoGame.GetPlayersCardInfo(CluedoGame.Players[i], card);
                    if (pci != null)
                    {
                        Bitmap bmp = null;
                        if (pci.PlayerHasCard.HasValue && pci.PlayerHasCard.Value == true)
                            bmp = Properties.Resources.Yes;
                        if (pci.PlayerHasCard.HasValue && pci.PlayerHasCard.Value == false)
                            bmp = Properties.Resources.No;

                        if (bmp != null)
                            row.Cells[i + 1].Value = bmp;
                        row.Cells[i + 1].ToolTipText = pci.ToolTip;

                        row.Cells[i + 1].ErrorText = pci.ErrorText;

                        //row.Cells[i + 1].Style.BackColor = pci.Player.Color;

                        if (card.Equals(CluedoGame.SuspectCardSolution) || card.Equals(CluedoGame.WeaponCardSolution) || card.Equals(CluedoGame.RoomCardSolution))
                            row.Cells[0].Style.ApplyStyle(solutionStyle);
                    }
                }
                dataGridView1.Rows.Add(row);
            }


            // Solution
            if (CluedoGame.SuspectCardSolution != null)
                labelSuspect.Text = CluedoGame.SuspectCardSolution.Name;
            if (CluedoGame.WeaponCardSolution != null)
                labelWeapon.Text = CluedoGame.WeaponCardSolution.Name;
            if (CluedoGame.RoomCardSolution != null)
                labelRoom.Text = CluedoGame.RoomCardSolution.Name;

            // Status strip
            tsslPossibleSuspectsCount.Text = CluedoGame.SuspectsLeftCount.ToString();
            tsslPossibleWeaponsCount.Text = CluedoGame.WeaponsLeftCount.ToString();
            tsslPossibleRoomsCount.Text = CluedoGame.RoomsLeftCount.ToString();

            tsslTableCompleteness.Text = string.Format("{0:P1}", CluedoGame.TableCompleteness);

            tsslErrors.Text = CluedoGame.ErrorCount.ToString();
        }
    }
}
